package com.wangsong.system.controller;


import com.wangsong.common.model.CodeEnum;
import com.wangsong.common.model.GetEasyUIData;
import com.wangsong.common.model.Result;
import com.wangsong.system.entity.User;
import com.wangsong.system.service.IUserService;
import com.wangsong.system.vo.UserAddVO;
import com.wangsong.system.vo.UserPageVO;
import com.wangsong.system.vo.UserUpdateVO;
import com.wangsong.system.vo.UserVO;

import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import com.wangsong.common.controller.BaseController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-09-19
 */
@RestController
@RequestMapping("/system/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;


    @PreAuthorize("hasAuthority('/system/user/list')")
    @RequestMapping(value = "/list")
    public Result list(@RequestBody UserPageVO user) {
        GetEasyUIData list = userService.list(user);
        return Result.success(list);
    }

    @PreAuthorize("hasAuthority('/system/user/add')")
    @RequestMapping(value = "/add")
    public Result add(@Valid @RequestBody UserAddVO user) {
        userService.add(user);
        return Result.success();

    }


    @RequestMapping(value = "/selectByPrimaryKey")
    public Result selectByPrimaryKey(Long id) {
        UserVO userVO = userService.selectVOByPrimaryKey(id);
        return Result.success(userVO);
    }

    @PreAuthorize("hasAuthority('/system/user/update')")
    @RequestMapping(value = "/update")
    public Result update(@Valid @RequestBody UserUpdateVO muser) {
        userService.updateUser(muser);
        return Result.success();

    }


    @PreAuthorize("hasAuthority('/system/user/delete')")
    @RequestMapping(value = "/delete")
    public Result delete(@RequestBody Long[] ids) {
        userService.deleteUser(ids);
        return Result.success();

    }

    @RequestMapping(value = "/findUserByUser")
    public Result findUserByUser(@RequestBody User user) {
        Boolean r = userService.findTByT(user);
        return Result.success(r);

    }

    @RequestMapping(value = "/toUpdatePassword")
    public Result toUpdatePassword() {
        User tByUsername = userService.findTByUsername(getUser());
        return Result.success(tByUsername);
    }

    @RequestMapping(value = "/updatePassword")
    public Result updatePassword(@RequestBody UserAddVO muser) {
        userService.updatePassword(muser);
        return Result.success();

    }

    @RequestMapping(value = "/findResourcesEMUByResources")
    public Result findResourcesEMUByResources() {

        return Result.success(userService.findResourcesEMUByResources(getUser()));
    }
}
